﻿@inject I18n I18n

<span class="m-app-tooltip-btn">
    <MTooltip Bottom
              ContentClass="m-app-tooltip-btn__content"
              OpenDelay="200">
        <ActivatorContent>
            <MButton Icon
                     Class="@($"default-app-btn {Class}")"
                     Disabled="Disabled"
                     Href="@Href"
                     Loading="@_loading"
                     Target="@Target"
                     OnClick="@HandleOnClick"
                     aria-label="@Path"
                     @attributes="@context.Attrs">
                <MBadge Dot Color="@DotColor" Value="Dot">
                    <MIcon SvgAttributes="@IconSvgAttrs">@Icon</MIcon>
                </MBadge>
            </MButton>
        </ActivatorContent>
        <ChildContent>
            @I18n.T(Path)
        </ChildContent>
    </MTooltip>
</span>

@code {

    [Parameter, EditorRequired]
    public string Icon { get; set; } = null!;

    [Parameter]
    public Dictionary<string, object?>? IconSvgAttrs { get; set; }

    [Parameter, EditorRequired]
    public string? Path { get; set; }

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public string? Href { get; set; }

    [Parameter]
    public string? Target { get; set; }

    [Parameter]
    public string? Class { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    [Parameter]
    public bool Dot { get; set; }

    [Parameter]
    public string? DotColor { get; set; }

    private bool _loading;

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        await base.SetParametersAsync(parameters);

        Icon.ThrowIfNull("AppTooltipButton");
    }

    private async Task HandleOnClick(MouseEventArgs args)
    {
        if (OnClick.HasDelegate)
        {
            _loading = true;
            StateHasChanged();

            await OnClick.InvokeAsync(args);
        }

        _loading = false;
    }

}
